package com.ruoyi.hmy.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.hmy.domain.HmyShopsInfo;
import com.ruoyi.hmy.mapper.HmyShopsMapper;
import com.ruoyi.hmy.domain.HmyShops;
import com.ruoyi.hmy.service.IHmyShopsService;

import javax.annotation.Resource;

/**
 * 商户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-15
 */
@Service
public class HmyShopsServiceImpl extends ServiceImpl<HmyShopsMapper,HmyShops> implements IHmyShopsService {

    @Resource
    private HmyShopsMapper hmyShopsMapper;

    /**
     * 查询商户信息
     *
     * @param shopId 商户信息主键
     * @return 商户信息
     */
    @Override
    public HmyShops selectHmyShopsByShopId(Long shopId) {
        return hmyShopsMapper.selectHmyShopsByShopId(shopId);
    }

    /**
     * 查询商户信息列表
     *
     * @param hmyShops 商户信息
     * @return 商户信息
     */
    @Override
    public List<HmyShops> selectHmyShopsList(HmyShops hmyShops) {
        return hmyShopsMapper.selectHmyShopsList(hmyShops);
    }

    /**
     * 新增商户信息
     *
     * @param hmyShops 商户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHmyShops(HmyShops hmyShops) {
        int rows = hmyShopsMapper.insertHmyShops(hmyShops);
        insertHmyShopsInfo(hmyShops);
        return rows;
    }

    /**
     * 修改商户信息
     *
     * @param hmyShops 商户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHmyShops(HmyShops hmyShops) {
        hmyShops.setUpdateTime(LocalDateTime.now());
        hmyShopsMapper.deleteHmyShopsInfoByShopId(hmyShops.getShopId());
        insertHmyShopsInfo(hmyShops);
        return hmyShopsMapper.updateHmyShops(hmyShops);
    }

    /**
     * 批量删除商户信息
     *
     * @param shopIds 需要删除的商户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHmyShopsByShopIds(Long[] shopIds) {
        hmyShopsMapper.deleteHmyShopsInfoByShopIds(shopIds);
        return hmyShopsMapper.deleteHmyShopsByShopIds(shopIds);
    }

    /**
     * 删除商户信息信息
     *
     * @param shopId 商户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHmyShopsByShopId(Long shopId) {
        hmyShopsMapper.deleteHmyShopsInfoByShopId(shopId);
        return hmyShopsMapper.deleteHmyShopsByShopId(shopId);
    }

    /**
     * 新增商户详情信息
     *
     * @param hmyShops 商户信息对象
     */
    public void insertHmyShopsInfo(HmyShops hmyShops) {
        HmyShopsInfo hmyShopsInfo = hmyShops.getHmyShopsInfo();
        Long shopId = hmyShops.getShopId();
        List<HmyShopsInfo> list = new ArrayList<HmyShopsInfo>();
        if (StringUtils.isNull(hmyShopsInfo.getId())) {
            hmyShopsInfo.setCreateTime(LocalDateTime.now());
            hmyShopsInfo.setUpdateTime(LocalDateTime.now());
            hmyShopsInfo.setDelFlag("0");
            hmyShopsInfo.setId(shopId);
        }else {
            hmyShopsInfo.setUpdateTime(LocalDateTime.now());
        }
        list.add(hmyShopsInfo);
        if (list.size() > 0) {
            hmyShopsMapper.batchHmyShopsInfo(list);
        }
    }
}
